/**
    Copyright (C) 2012 Guido Ludwig

    This file is part of JATT.
	http://code.google.com/p/jatt/

    JATT is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.guidoludwig.jatt.chain;

import org.apache.commons.chain.Chain;
import org.apache.commons.chain.impl.ChainBase;

import de.guidoludwig.jatt.chain.commands.CleanupCommand;
import de.guidoludwig.jatt.chain.commands.ClearCommand;
import de.guidoludwig.jatt.chain.commands.CollectArtworkCommand;
import de.guidoludwig.jatt.chain.commands.ExtractArtworkCommand;
import de.guidoludwig.jatt.chain.commands.FolderScanCommand;
import de.guidoludwig.jatt.chain.commands.LastFMArtworkCommand;
import de.guidoludwig.jatt.chain.commands.SelectOptimizedImageCommand;
import de.guidoludwig.jatt.chain.commands.ValidationCommand;
import de.guidoludwig.jatt.chain.commands.WriteArtworkCommand;

/**
 * @author Guido Ludwig
 */
public class JATTChain {

	private final Chain chain;

	private final FolderScanCommand scanCommand;
	private final ExtractArtworkCommand extractCommand;
	private final ClearCommand clearCommand;
	private final LastFMArtworkCommand lastfmCommand;
	private final SelectOptimizedImageCommand selectOptimizedCommand;
	private final CleanupCommand cleanupCommand;
	private final WriteArtworkCommand writeArtCommand;
	private final ValidationCommand validationCommand;
	private final CollectArtworkCommand collectCommand;

	public JATTChain() {
		chain = new ChainBase();
		scanCommand = new FolderScanCommand();
		extractCommand = new ExtractArtworkCommand();
		clearCommand = new ClearCommand();
		lastfmCommand = new LastFMArtworkCommand();
		selectOptimizedCommand = new SelectOptimizedImageCommand();
		writeArtCommand = new WriteArtworkCommand();
		validationCommand = new ValidationCommand();
		cleanupCommand = new CleanupCommand();
		collectCommand = new CollectArtworkCommand();

		chain.addCommand(scanCommand);
		chain.addCommand(extractCommand);
		chain.addCommand(clearCommand);
		chain.addCommand(lastfmCommand);
		chain.addCommand(selectOptimizedCommand);
		chain.addCommand(writeArtCommand);
		chain.addCommand(collectCommand);
		chain.addCommand(validationCommand);
		chain.addCommand(cleanupCommand);
	}

	public boolean execute(JATTContext context) throws Exception {
		validateConfiguration();
		return chain.execute(context);
	}

	private void validateConfiguration() {
		if (!scanCommand.isConfigured() || !extractCommand.isConfigured()
				|| !clearCommand.isConfigured()
				|| !lastfmCommand.isConfigured()
				|| !selectOptimizedCommand.isConfigured()
				|| !cleanupCommand.isConfigured()
				|| !writeArtCommand.isConfigured()
				|| !collectCommand.isConfigured()
				|| !validationCommand.isConfigured()) {
			throw new IllegalStateException("Chain is not configured !");
		}

	}

	public FolderScanCommand getScanCommand() {
		return scanCommand;
	}

	public ExtractArtworkCommand getExtractCommand() {
		return extractCommand;
	}

	public ClearCommand getClearCommand() {
		return clearCommand;
	}

	public LastFMArtworkCommand getLastfmCommand() {
		return lastfmCommand;
	}

	public SelectOptimizedImageCommand getSelectOptimizedCommand() {
		return selectOptimizedCommand;
	}

	public CollectArtworkCommand getCollectArtworkCommand() {
		return collectCommand;
	}

	public CleanupCommand getCleanupCommand() {
		return cleanupCommand;
	}

	public WriteArtworkCommand getWriteArtCommand() {
		return writeArtCommand;
	}

	public ValidationCommand getValidationCommand() {
		return validationCommand;
	}
}
